#include "LutQgisXmlFile.h"

#include "iostream"
#include "tinyxml.h"

using namespace tinyxml;

LutQgisXmlFile::LutQgisXmlFile()
{}

void
LutQgisXmlFile::open(std::string filename)
{
    TiXmlDocument doc(filename.c_str());
    if(!doc.LoadFile()){
            std::cerr << "error #" << doc.ErrorId() << " : " << doc.ErrorDesc()
                      << std::endl;
    }
    else{
        m_filename=filename;
    }
}

boost::shared_ptr<LutModel>
LutQgisXmlFile::loadLut()
{

    boost::shared_ptr<LutModel> lutmodel_ptr=boost::shared_ptr<LutModel>() ;
    TiXmlDocument doc(m_filename.c_str());
    if(!doc.LoadFile()){
            std::cerr<< "erreur lors du chargement" << std::endl;
            std::cerr << "error #" << doc.ErrorId() << " : " << doc.ErrorDesc()
                      << std::endl;
    }

    // read file
    TiXmlHandle hdl(&doc);
    TiXmlElement* pElem;
    //pElem=hdl.FirstChild("lut").Element();


    return lutmodel_ptr;
}

void
LutQgisXmlFile::saveAs( const boost::shared_ptr<LutModel> lut_ptr ,
                       std::string filename)
{
    //  creation d'un nouveau document xml
    TiXmlDocument doc;
    //  ecriture de l'en-tête
    TiXmlElement* msg;
    TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );
    doc.LinkEndChild( decl );
    //
    doc.SaveFile(filename.c_str() ); // enregistrement du fichier exporté
}

